<?php

/**
 * descript:
 * User: blue
 * Date: 2018/9/13 16:47
 */

namespace app\library\base;

use app\library\server\Authority;
use app\library\server\TokenS;
use think\Controller;
use think\facade\Request;

class BaseController extends Controller
{
    protected $validation;
    protected $model;
    //$validname必须为完整的namesapce路径
    protected $validname;
    protected $modelName;

    public function __construct()
    {
        $this->validation=new $this->validname();
        $this->model=new $this->modelName();
        parent::__construct();
    }

    /*************************基础curd*****************************/
    /*添加规则*/
    public function addc(){
        $data=Request::post();
        $this->validation->goCheck($data,'post');
        //模型灵活使用
        $res=$this->model->add($data);
        return successStatus('ok',$res);
    }
    /*数据编辑*/
    public function updatec(){
        $data=Request::post();
        $this->validation->goCheck($data,'put');
        $res=$this->model->edit($data);
        return successStatus('ok',$res);
    }
    /*数据删除*/
    public function delc(){
        $ids=Request::post();
        $this->validation->goCheck($ids,'delete');
        $res=$this->model->del($ids['id']);
        return successStatus('ok',$res);
    }

    /**
     * @params page 显示页码
     * @params limit 每页显示的条数
     * @params keyword 搜索条件
     * @return array
     */
    public function listc(){
        $data=Request::param();
        $keyword=empty($data['keyword'])?'':$data['keyword'];
        $page=empty($data['page'])?1:$data['page'];
        $limit=empty($data['limit'])?'':$data['limit'];
        $list=$this->model->lists($page,$limit,$keyword);
        if($list)
            return successStatus('ok',$list);
    }


    /**************************权限控制***********************/
    //验证token 是否有效
    final protected function tokenIsValid(){
        TokenS::tokenVerify();
    }
    //验证权限
    final protected function checkAuth(){
        Authority::checkAuth();
    }

}